<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="区县" prop="qxdm">
        <el-select v-model="queryParams.qxdm" placeholder="请选择" clearable size="small">
          <el-option
            v-for="dict in qyList"
            :key="dict.v"
            :label="dict.n"
            :value="dict.v"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="中学代码" prop="zxdm">
        <el-input
          v-model="queryParams.zxdm"
          placeholder="请输入中学代码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="中学名称" prop="zxmc">
        <el-input
          v-model="queryParams.zxmc"
          placeholder="请输入中学名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增学校</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="schoolList" >
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="区县名称" align="center" prop="qxmc" />
      <el-table-column label="中学代码" align="center" prop="zxdm" />
      <el-table-column label="中学名称" align="center" prop="zxmc" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >编辑</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改学校信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="区县" prop="qxdm">
          <el-select v-model="form.qxdm" placeholder="请选择"  size="small">
            <el-option
              v-for="dict in qyList"
              :key="dict.v"
              :label="dict.n"
              :value="dict.v"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="中学代码" prop="zxdm">
          <el-input v-model="form.zxdm" placeholder="请输入中学代码" :disabled="disabled"/>
        </el-form-item>
        <el-form-item label="中学名称" prop="zxmc">
          <el-input v-model="form.zxmc" placeholder="请输入中学名称" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input type="textarea" :rows="2" v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
    import { listSchool, getSchool, delSchool, addSchool, updateSchool, getSuZhouQuYu } from "@/api/business/school/school";

    export default {
        name: "School",
        data() {
            return {
                // 遮罩层
                loading: true,
                // 导出遮罩层
                exportLoading: false,
                // 选中数组
                ids: [],
                // 非单个禁用
                single: true,
                // 非多个禁用
                multiple: true,
                // 显示搜索条件
                showSearch: true,
                // 总条数
                total: 0,
                // 学校信息表格数据
                schoolList: [],
                //区域列表
                qyList:[],
                // 弹出层标题
                title: "",
                // 是否显示弹出层
                open: false,
                // 查询参数
                queryParams: {
                    pageNum: 1,
                    pageSize: 10,
                    qxdm: null,
                    zxdm: null,
                    zxmc: null,
                    created: null,
                    updated: null
                },
                // 表单参数
                form: {},
                //禁用
                disabled:false,
                // 表单校验
                rules: {
                    qxdm: [
                        { required: true, message: "区县代码不能为空", trigger: "blur" }
                    ],
                    zxdm: [
                        { required: true, message: "中学代码不能为空", trigger: "blur" },
                        { pattern: /^[0-9a-zA-Z]+$/, message: '中学代码只能输入数字或英文字母' }
                    ],
                    zxmc: [
                        { required: true, message: "中学名称不能为空", trigger: "blur" }
                    ]
                }
            };
        },
        created() {
            this.getList();
            getSuZhouQuYu().then(response => {
                this.qyList = response.data;
            });
        },
        methods: {
            /** 查询学校信息列表 */
            getList() {
                this.loading = true;
                listSchool(this.queryParams).then(response => {
                    this.schoolList = response.rows;
                    this.total = response.total;
                    this.loading = false;
                });
            },
            // 取消按钮
            cancel() {
                this.open = false;
                this.reset();
            },
            // 表单重置
            reset() {
                this.form = {
                    id: null,
                    qxdm: null,
                    zxdm: null,
                    zxmc: null,
                    remark: null
                };
                this.resetForm("form");
            },
            /** 搜索按钮操作 */
            handleQuery() {
                this.queryParams.pageNum = 1;
                this.getList();
            },
            /** 重置按钮操作 */
            resetQuery() {
                this.resetForm("queryForm");
                this.handleQuery();
            },
            /** 新增按钮操作 */
            handleAdd() {
                this.reset();
                this.disabled=false;
                this.open = true;
                this.title = "添加学校信息";
            },
            /** 修改按钮操作 */
            handleUpdate(row) {
                this.reset();
                this.disabled=true;
                const id = row.id || this.ids
                getSchool(id).then(response => {
                    this.form = response.data;
                    this.open = true;
                    this.title = "修改学校信息";
                });
            },
            /** 提交按钮 */
            submitForm() {
                this.$refs["form"].validate(valid => {
                    if (valid) {
                        if (this.form.id != null) {
                            updateSchool(this.form).then(response => {
                                this.$modal.msgSuccess("修改成功");
                                this.open = false;
                                this.getList();
                            });
                        } else {
                            addSchool(this.form).then(response => {
                                this.$modal.msgSuccess("新增成功");
                                this.open = false;
                                this.resetQuery();
                            });
                        }
                    }
                });
            },
            /** 删除按钮操作 */
            handleDelete(row) {
                const ids = row.id || this.ids;
                this.$modal.confirm('是否确认删除学校信息编号为"' + ids + '"的数据项？').then(function() {
                    return delSchool(ids);
                }).then(() => {
                    this.getList();
                    this.$modal.msgSuccess("删除成功");
                }).catch(() => {});
            }
        }
    };
</script>
